package api

import (
	"context"
	"errors"
	"goods_srv/models"
	"goods_srv/models/dao"
	pb "goods_srv/proto"
)

type GoodsServer struct {
	pb.UnimplementedGoodsServer
}

func (g *GoodsServer) GoodsAdd(ctx context.Context, req *pb.GoodsAddRequest) (resp *pb.GoodsAddResponse, err error) {

	goods := models.Product{
		Name:        req.Name,
		Code:        req.Code,
		Price:       float64(req.Price),
		CategoryId:  int(req.CategoryId),
		Status:      int(req.Status),
		Image:       req.Image,
		Videos:      req.Videos,
		IsHot:       req.IsHot,
		Unit:        req.Unit,
		Description: req.Description,
	}
	err = dao.CreateGoods(&goods)
	if err != nil {
		return nil, errors.New("error" + "商品添加失败")
	}
	return &pb.GoodsAddResponse{Success: true}, nil
}
